﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Runtime.Serialization;
using System.Xml;
using System.Text;
using System.IO;
using System.Windows;

namespace Sonce.Database
{
	//NOTE: ElementPrototype is resistor, Element is instance or resistor (R1, R2, Rn)
	public class Repository
	{
		#region Tables
		public static Table<Document> Documents = new Table<Document>()
		{
			TableName = "Document",
			AutoIncrement = true
		};

		public static Table<Element> Elements = new Table<Element>()
		{
			TableName = "Element",
			AutoIncrement = true
		};

		public static Table<ElementPrototype> ElementPrototypes = new Table<ElementPrototype>()
		{
			TableName = "ElementPrototype",
			AutoIncrement = false
		};
		#endregion

		#region Import/Export
		public static void Import()
		{
			XDocument xml = XDocument.Parse("<html></html>");

			//var elements =
			//    from x in xml.Descendants("html")
			//select x;
			//select new Element
			//{
			//    ID = x.Element("ID")
			//};
			//select new
			//{
			//    ProductID = Convert.ToInt32(product.Element("ProductId").Value),
			//    ProductName = (string)product.Element("ProductName").Value
			//};

			//foreach (var element in elements)
			//{
			//    //element.
			//}
		}

		public static string Export()
		{
			XDocument xml = new XDocument();
			XElement root = new XElement("Database");

			//LoadTestData();
			//string result = null;
			//using (MemoryStream stream = new MemoryStream())
			//{
			//    //StringBuilder output = new StringBuilder();
			//    //XmlWriter writer = XmlWriter.Create(output);
			//    DataContractSerializer serializer = new DataContractSerializer(typeof(ElementPrototype), "ElementPrototypes", "http://mynamespace/");
			//    serializer.WriteObject(stream, ElementPrototypes);
			//    //serializer.WriteObject(writer, ElementPrototypes);
			//    stream.Position = 0;
			//    using (StreamReader reader = new StreamReader(stream))
			//    {
			//        result = reader.ReadToEnd();
			//    }
			//}

			//#region ElementPrototype
			//XElement xElementPrototypes = new XElement("ElementPrototypes");
			//foreach (ElementPrototype item in ElementPrototypes)
			//{
			//    xElementPrototypes.Add(item);
			//}
			//root.Add(xElementPrototypes);
			//#endregion

			//#region Element
			//XElement xElements = new XElement("Elements");
			//foreach (Element item in Elements)
			//{
			//    XElement xElement = new XElement("Element");
			//    xElement.Add(
			//        new XAttribute("ID", item.ID),
			//        new XAttribute("Name", item.Name),
			//        new XAttribute("Label", item.Label));
			//    xElements.Add(xElement);
			//}
			//root.Add(xElements);
			//#endregion

			xml.Add(root);
			return xml.ToString();
		}

		public static void LoadTestData()
		{
			Repository.ElementPrototypes.Insert(new ElementPrototype()
			{
				ID = 101,
				//Image = Loader.LoadResourceString("Library/Resistor.xaml"),
				Name = "Capacitor",
				Nodes = new List<Point>()
			});

			Repository.ElementPrototypes.Insert(new ElementPrototype()
			{
				ID = 102,
				//Image = Loader.LoadResourceString("Library/Capacitor.xaml"),
				Name = "Capacitor",
				Nodes = new List<Point>()
			});

			Repository.ElementPrototypes.Insert(new ElementPrototype()
			{
				ID = 103,
				//Image = Loader.LoadResourceString("Library/Inductor.xaml"),
				Name = "Inductor",
				Nodes = new List<Point>()
			});

			Repository.ElementPrototypes.Insert(new ElementPrototype()
			{
				ID = 104,
				//Image = Loader.LoadResourceString("Library/Source.xaml"),
				Name = "Source",
				Nodes = new List<Point>()
			});
		}
		#endregion
	}
}
